package com.gblfy.qywxin.utils;

import com.gblfy.qywxin.vo.QywxInnerUser;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;

public class JWTUtils {

    private static final long EXPIRE = 60000 * 60 * 24 * 7;
    private static final String SECRET = "tobdev.com";


    private static final String TOKEN_PREFIX = "tobdev";

    private static final String SUBJECT = "tobdev";

    /**
     * 生成jwt token
     *
     * @param user
     * @return
     */
    public static String geneJsonWebToken(QywxInnerUser user) {
        String token = Jwts.builder().setSubject(SUBJECT)
                .claim("corp_id", user.getCorpId())
                .claim("user_id", user.getUserId())
                .claim("user_name", user.getName())
                .claim("mobile", user.getMobile())
                .claim("qr_code", user.getQrCode())
                .claim("user_type", user.getUserType())
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis() + EXPIRE))
                .signWith(SignatureAlgorithm.HS256, SECRET).compact();
        token = TOKEN_PREFIX + token;
        return token;
    }

    /**
     * 校验token是否合法
     *
     * @param token
     * @return
     */
    public static Claims checkJWT(String token) {
        try {
            final Claims claims = Jwts.parser().setSigningKey(SECRET).parseClaimsJws(token.replace(TOKEN_PREFIX, "")).getBody();
            return claims;
        } catch (Exception e) {
            return null;
        }
    }
}
